package com.lufei.dh.bottom.Fragment;

import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.lufei.dh.R;
import com.lufei.dh.bottom.Adapter.Adapter_Drag;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 版权：易金卡沃 版权所有
 * <p>
 * 作者：lufei
 * <p>
 * 创建日期：2018/6/25
 * <p>
 * 描述：添加拖拽、滑动删除
 * <p>
 * 修订历史：
 */
public class Fragment1 extends Fragment {
    @BindView(R.id.f1_recyclerview)
    RecyclerView f1Recyclerview;
    Unbinder unbinder;
    Adapter_Drag adapterDrag;
    private List<String> data = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        Log.e("lufei", "1111");
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment1, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void setData() {
        int xv = 15;
        for (int i = 0; i < xv; i++) {
            data.add("数据" + i);
        }
    }

    private void init() {
        setData();
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        adapterDrag=new Adapter_Drag(data,getContext());
        f1Recyclerview.setLayoutManager(linearLayoutManager);
        f1Recyclerview.setAdapter(adapterDrag);

        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(adapterDrag);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(f1Recyclerview);

// 开启拖拽
        adapterDrag.enableDragItem(itemTouchHelper, R.id.tv, true);
        adapterDrag.setOnItemDragListener(new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {

            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {

            }
        });

// 开启滑动删除
        adapterDrag.enableSwipeItem();
        adapterDrag.setOnItemSwipeListener(new OnItemSwipeListener() {
            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {

            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("lufei", "1111.....");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
